﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;

namespace Diplomova_prace
{
    public class SaveDialogs
    {
        Exports export = new Exports();
        string destinationFile = string.Empty;
        public string PathToDestinationFileProperty
        {
            get { return destinationFile; }
        }
        public bool SaveAsHTML(string input)
        {
            if (input != string.Empty)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "HyperText Markup Language | *.html";
                sfd.DefaultExt = "html";
                string destination = string.Empty;
                if (DialogResult.OK == sfd.ShowDialog())
                {
                    destination = sfd.FileName;
                    if (destination != string.Empty)
                    {
                        Thread thread = new Thread(() => export.HTML(input, destination));
                        thread.Start();
                    }
                    destinationFile = Path.GetFullPath(destination);
                    return true;
                }
                return false;
            }
            else
            {
                MessageBox.Show("Pro export musí být nejdříve proveden výpočet", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }
        public bool SaveAsXML(string input)
        {
            if (input != string.Empty)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "eXtensible Markup Language | *.xml";
                sfd.DefaultExt = "xml";
                string destination = string.Empty;
                if (DialogResult.OK == sfd.ShowDialog())
                {

                    destination = sfd.FileName;
                    if (destination != string.Empty)
                    {
                        Thread thread = new Thread(() => export.XML(input, destination));
                        thread.Start();
                    }
                    destinationFile = Path.GetFullPath(destination);
                    return true;
                }
                return false;
            }
            else
            {
                MessageBox.Show("Pro export musí být nejdříve proveden výpočet", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }
        public bool SaveAsImage(string input,int width,int height, int fontSize, string pathToHtml)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (input != string.Empty)
            {
                sfd.Filter = "JPEG soubory (*.jpg) (*.jpeg) | *.jpg *.jpeg| PNG soubory (*.png)| *.png| BMP soubory (*.bmp)| *.bmp|GIF soubory (*.gif)| *.gif| TIFF soubory (*.tiff) (*tif)| *.tiff *.tif";
                string destination = string.Empty;
                if (DialogResult.OK == sfd.ShowDialog())
                {
                    destination = sfd.FileName;
                    if (destination != string.Empty)
                    {
                        ConvertToImage(destination, pathToHtml, width, height, fontSize, input);
                        destinationFile = destination;
                        return true;
                    }
                }
                return false;
            }
            else
            {
                MessageBox.Show("Pro export musí být nejdříve proveden výpočet", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            } 
        }
        private void ConvertToImage(string destination, string source, int width, int height, int fontsize,string input)
        {
            if (destination != string.Empty)
            {
                string extension = Path.GetExtension(destination);
                if (extension == ".jpg" || extension == ".jpeg")
                    export.Image(input, destination, width, height, ImageFormat.Jpeg, fontsize);
                else if (extension == ".png")
                    export.Image(input, destination, width, height, ImageFormat.Png, fontsize);
                else if (extension == ".bmp")
                    export.Image(input, destination, width, height, ImageFormat.Bmp, fontsize);
                else if (extension == ".gif")
                    export.Image(input, destination, width, height, ImageFormat.Gif, fontsize);
                else if (extension == ".tiff" || extension == ".tif")
                    export.Image(input, destination, width, height, ImageFormat.Tiff, fontsize);
                
            }
        }
    }
}
